package com.mlh.fileupload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadHandler {
	//处理文件上传的请求
	@RequestMapping("/fileUpload")
	public String fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
		//接受到提交的文件名
		String originalFilename = file.getOriginalFilename();
		System.out.println("你上传的文件名="+originalFilename);
		//获取 文件要上传的路径[全路径：包括文件名]
		String fileFullPath = request.getServletContext().getRealPath("/img/" + originalFilename);
		File saveToFile = new File(fileFullPath);
		//将上传的文件，转存到saveToFile
		file.transferTo(saveToFile);
		return "success";
	}
}
